﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services.Common
{
    public class ISMServiceParameter
    {
        public object key{get;set;}
        public object value{get;set;}
        public static ISMServiceParameter New(object _key,object _value)
        {
            ISMServiceParameter para = new ISMServiceParameter()
            {
                key = _key,
                value = _value
            };
            return para;
        }
    }
    public class ISMServiceParameterCollection
    {
        private List<ISMServiceParameter> collection { get; set; }

        public ISMServiceParameterCollection()
        {
            collection = new List<ISMServiceParameter>();
        }
        public bool Add(object key,object value)
        {
            int index = collection.FindIndex((p)=>
                {
                    return key.Equals(p.key);
                });
            if(index == -1)
                return false;
            ISMServiceParameter para = ISMServiceParameter.New(key,value);
            collection.Add(para);
            return true;
        }
        public bool Delete(object key)
        {
            int index = collection.FindIndex(p=>{
                return p.key.Equals(key);
            });
            if(index == -1)
                return false;
            collection.RemoveAt(index);
            return true;
        }
        public bool DeleteItemAtIndex(int index)
        {
            if (index < 0 || index > (collection.Count - 1))
                return false;
            collection.RemoveAt(index);
            return true;
        }
        public ISMServiceParameter FoundItemAtIndexWithKey(object key)
        {
            int index = collection.FindIndex(p=>
                {
                    return p.key.Equals(key);
                });
            if(index == -1)
                return null;
            return collection[index];
        }
        public ISMServiceParameter FoundItemWithPassingTest(Predicate<ISMServiceParameter> predictor)
        {
            ISMServiceParameter foundItem = null;
            collection.ForEach(p =>
                {
                    if(predictor(p))
                    {
                        foundItem = p;
                        return;
                    }
                });
            return foundItem;
        }

        public object this[object key]
        {
            get
            {
                ISMServiceParameter param = this.FoundItemAtIndexWithKey(key);
                if (param == null)
                    return null;
                return param.value;
            }
            set
            {
                ISMServiceParameter param = this.FoundItemAtIndexWithKey(key);
                if(param == null)
                {
                    param = ISMServiceParameter.New(key, value);
                }
                else
                {
                    param.value = value;
                }
            }
        }
    }
}
